package com.xiaotu.easypoi.testExportMoreSheet.controller;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.enmus.ExcelType;
import com.xiaotu.easypoi.testExportMoreSheet.model.AnimalModel;
import com.xiaotu.easypoi.testExportMoreSheet.model.StudentModel;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author 张晓
 * @create 2021-09-26 21:34
 */
@RestController
@RequestMapping
public class ExportMoreSheetExcelController {

    @RequestMapping("exportMoreSheetExcel")
    public void exportMoreSheetExcel() {
        List<Map<String, Object>> list = new ArrayList<>();
        Map<String, Object> sheet1Map = new HashMap<>();
        ExportParams exportParams1 = new ExportParams();
        exportParams1.setSheetName("1小组");
        sheet1Map.put("title", exportParams1);
        sheet1Map.put("entity", StudentModel.class);
        List<StudentModel> studentModelList = new ArrayList<>();
        studentModelList.add(new StudentModel("张三1", 1));
        studentModelList.add(new StudentModel("张三2", 2));
        studentModelList.add(new StudentModel("张三3", 1));
        sheet1Map.put("data", studentModelList);
        Map<String, Object> sheet2Map = new HashMap<>();
        sheet2Map.put("title", new ExportParams("计算机一班学生", "2小组"));
        sheet2Map.put("entity", AnimalModel.class);
        List<AnimalModel> animalModelList = new ArrayList<>();
        animalModelList.add(new AnimalModel(110, 119));
        animalModelList.add(new AnimalModel(120, 119));
        animalModelList.add(new AnimalModel(150, 139));
        sheet2Map.put("data", animalModelList);
        list.add(sheet1Map);
        list.add(sheet2Map);
        Workbook workbook = null;
        FileOutputStream os = null;
        try {
            workbook = ExcelExportUtil.exportExcel(list, ExcelType.XSSF);
            os = new FileOutputStream(new File("E:\\360MoveData\\Users\\Administrator\\Desktop\\exportExcelMoreSheet.xlsx"));
            workbook.write(os);
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            if (null != os) {
                try {
                    os.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
    }

}
